package com.sxt.sys.controller;


import com.sxt.sys.domain.User;
import com.sxt.sys.service.TopicService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.vo.TopicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cr
 * @since 2019-04-29
 */
@Controller
public class TopicController {
    @Autowired
    private TopicService topicService;



    @RequestMapping("/login/login")
    public String login(String loginname,String password){
        User u = topicService.queryOne(loginname,password);
        if(u!=null){
            return "index";
        }else {
            return "login";
        }

    }

    @RequestMapping("/newOne")
    public String newOne(){

        return "newOne";
    }
    @RequestMapping("/topic/loadAllTopic")
    @ResponseBody
    public DataGridView loadAll(TopicVo topicVo){

        return topicService.queryAll(topicVo);
    }
    @RequestMapping("/topic/addTopic")
    @ResponseBody
    public ResultObj addTopic(TopicVo topicVo,String anthour,String contents){
        topicVo.setCreatedate(new Date());
        topicVo.setClickamount(0+"");
        Integer code=1;
        String msg="";
        try {
            Integer info = topicService.insert(topicVo);
            code = info;
            msg = info > 0 ?"发帖成功":"发帖失败";
        } catch (Exception e) {
            code = 0;
            msg = "发帖失败";
        }
        return new ResultObj(code , msg);
    }

}

